package yue.shen.android.demo.select

import android.graphics.Rect
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.entity.LocalMediaFolder
import com.luck.picture.lib.model.LocalMediaLoader
import com.luck.picture.lib.tools.StringUtils
import yue.shen.android.demo.R
import yue.shen.android.demo.databinding.ActivitySelectPictureBinding

class SelectPictureActivity : AppCompatActivity(), SelectPicListener, SelectFolderListener {

    private val spanCount = 3

    private lateinit var viewBinding: ActivitySelectPictureBinding

    private val adapter: SelectAdapter by lazy { SelectAdapter(ArrayList()) }
    private var images = ArrayList<LocalMedia>()
    private var foldersList = ArrayList<LocalMediaFolder>()
    private var mediaLoader: LocalMediaLoader? = null
    private var selectFolderPop: SelectFolderPop? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = ActivitySelectPictureBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)
        initView()
        initData()
    }

    private fun initView() {
        viewBinding.tvCancel.setOnClickListener {
            if (selectFolderPop?.isShowing == true) {
                selectFolderPop?.dismiss()
            } else {
                finish()
            }
        }
        viewBinding.tvNext.setOnClickListener {
            if (selectFolderPop?.isShowing == true) {
                selectFolderPop?.dismiss()
            } else {
                selectNext()
            }
        }
        viewBinding.tvTitle.setOnClickListener { selectFolder() }
        val spacing = dip2px(8f)
        viewBinding.recycleView.setHasFixedSize(true)
        viewBinding.recycleView.addItemDecoration(object : ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                val position = parent.getChildAdapterPosition(view)
                val column: Int = position % spanCount
                outRect.left = column * spacing / spanCount
                outRect.right = spacing - (column + 1) * spacing / spanCount
                if (position < spanCount) {
                    outRect.top = spacing
                }
                outRect.bottom = spacing
            }
        })
        viewBinding.recycleView.layoutManager = GridLayoutManager(this, spanCount)
        viewBinding.recycleView.adapter = adapter
        adapter.setSelectPicListener(this)
        selectFolderPop = SelectFolderPop(this, viewBinding.tvTitle)
        selectFolderPop?.setOnItemClickListener(this)
        StringUtils.modifyTextViewDrawable(
            viewBinding.tvTitle,
            resources.getDrawable(R.mipmap.orange_arrow_down, null),
            2
        )
    }

    private fun initData() {
        mediaLoader = LocalMediaLoader(this, 0, false, 0, 0)


        mediaLoader?.loadAllMedia { folders ->
            if (folders.size > 0) {
                foldersList.clear()
                foldersList.addAll(folders)
                val folder = folders[0]
                folder.isChecked = true
                val localImg = folder.images
                if (localImg.size >= images.size) {
                    images.clear()
                    images.addAll(localImg)
                    selectFolderPop?.bindFolder(folders as ArrayList<LocalMediaFolder>)
                }
            }
            adapter.updateData(images)
        }
    }

    private fun selectFolder() {
        if (selectFolderPop?.isShowing == true) {
            selectFolderPop?.dismiss()
        } else {
            selectFolderPop?.showAsDropDown(viewBinding.tvTitle)
        }
    }

    private fun selectNext() {
        val selectImageData = adapter.getSelectImageData()
    }

    private fun dip2px(dpValue: Float): Int {
        val scale = resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }

    override fun attainSelectCount(attain: Boolean) {
        viewBinding.tvNext.isEnabled = attain
        viewBinding.tvNext.setBackgroundResource(if (attain) R.color.color_B5A36A else R.color.color_AAB0B3)
    }

    override fun onItemClick(folderName: String, images: ArrayList<LocalMedia>) {
        viewBinding.tvTitle.text = folderName
        adapter.updateData(images)
        if (selectFolderPop?.isShowing == true) {
            selectFolderPop?.dismiss()
        }
    }
}